﻿using System;
using System.Collections.Generic;
using Ruici.Web.Services;
using Ruici.Web.Services.CRS_Service;
using Ruici.Web.Services.ServiceForWeb;
using Newtonsoft.Json.Linq;

namespace Ruici.BLL
{
    public class UserReservitionBll
    {
        private UserReservitionService userService = new UserReservitionService();
        private UserService userModelService = new UserService();

        public List<resvIntention> getResvIntentionList(int iPid)
        {
            List<resvIntention> resvIntentionList = userService.ResvIntention_GetList(iPid);

            return resvIntentionList;
        }

        public string UpdateResvIntention(int pid,
                    int type,
                    int hospId,
                    int remoteHospId,
                    int depId,
                    int depTypeId,
                    int doctorId,
                    string timePeriod,
                    DateTime resvDate,
                    string description,
                    string pictures,
                    string userIP,
                    string platform,
                    string OpenId,
                    string userInfoJson,
                    int operatorPid)
        {
            return userService.ResvIntention_Add(pid, type, hospId, remoteHospId, depId, depTypeId, doctorId, timePeriod, resvDate, description, pictures, userIP, platform, OpenId, userInfoJson, operatorPid);
        }

        public string getPatientInforManifest(int iPID)
        {
             return userService.GetPatientInfoList(iPID);
        }

        public List<FamilyMember> getPatientVisitorList(int iPid)
        {
            return userService.GetPationtVisitorList(iPid);
        }

        public bool AddPatientVisitor(int iPID,string editName, string editPhone, string IdCardNo, bool isDefault)
        {
            int iGenderDigit = int.Parse(IdCardNo.Substring(IdCardNo.Length - 2, 1));
            bool updateResult = false;
            string sGender = "";
            if (iGenderDigit % 2 == 0)
                sGender = "女";
            else
                sGender = "男";

            string sBirthDate = IdCardNo.Substring(6, 4) + "/" + IdCardNo.Substring(10, 2) + "/" + IdCardNo.Substring(12, 2);
            DateTime BirthDate = Convert.ToDateTime(sBirthDate);

            List<FamilyMember> patientList = getPatientVisitorList(iPID);

            if(patientList.Count == 0)
                isDefault = true;
            else
            {
                if (isDefault)
                {
                    foreach (FamilyMember patient in patientList)
                    {                 
                        if (patient.FamilyName != editName && patient.IsDefault)
                            updateResult = userService.UpatePatientVisitor(patient.Id, patient.FamilyName, patient.FamilyMobile, patient.IDCardNo, false, patient.Gender, Convert.ToDateTime(patient.Birthday)).Success;
                    }
                }
            }
                      
            bool addResult = userService.AddPatientVisitor(iPID,editName, editPhone, IdCardNo, isDefault, sGender, BirthDate).Success;

            return addResult;
        }

        public bool UpdatePatientVisitor(int iPid,int visitID, string editName, string editPhone, string IdCardNo, bool isDefault)
        {
            int iGenderDigit = int.Parse(IdCardNo.Substring(IdCardNo.Length - 2, 1));
            bool bResult = false;
            string sGender = "";
            if (iGenderDigit % 2 == 0)
                sGender = "女";
            else
                sGender = "男";

            string sBirthDate = IdCardNo.Substring(6, 4) + "/" + IdCardNo.Substring(10, 2) + "/" + IdCardNo.Substring(12, 2);
            DateTime BirthDate = Convert.ToDateTime(sBirthDate);

            List<FamilyMember> PatientVisitorList = getPatientVisitorList(iPid);

            if (PatientVisitorList.Count == 0)
                isDefault = true;

            if (visitID != 0)
                bResult = userService.UpatePatientVisitor(visitID, editName, editPhone, IdCardNo, isDefault, sGender, BirthDate).Success;              
            else
                bResult = userService.AddPatientVisitor(iPid, editName, editPhone, IdCardNo, isDefault, sGender, BirthDate).Success;

            return bResult;
        }

        public string GetUserCaseListByTrId(int iPid, int OrgId, int visitID)
        {
            return userService.GetUserCaseListByTrID(iPid, OrgId, visitID);
        }

        public bool DeletePatientVisitor(int visitID, int iPID)
        {
            bool updateResult = false;
            FamilyMember defaultPatient = null;
            bool bDeleteResult = userService.DeletePatientVisitor(visitID);

            if (bDeleteResult)
            {
                List<FamilyMember> patientList = getPatientVisitorList(iPID);

                bool allFalse = true;
                foreach (FamilyMember patient in patientList)
                {
                    if (patient.IsDefault)
                        allFalse = false;
                }

                if (allFalse && patientList.Count!=0)
                {
                    DateTime maxDate = patientList[0].CreateTime;
                    defaultPatient = patientList[0];
                    foreach (FamilyMember patient in patientList)
                    {
                        if (patient.CreateTime > maxDate)
                        {
                            maxDate = patient.CreateTime;
                            defaultPatient = patient;
                        }
                    }
                }

                if (defaultPatient != null)
                    updateResult = userService.UpatePatientVisitor(defaultPatient.Id, defaultPatient.FamilyName, defaultPatient.FamilyMobile, defaultPatient.IDCardNo, true, defaultPatient.Gender, Convert.ToDateTime(defaultPatient.Birthday)).Success;
            }
            
            return bDeleteResult;
        }

        public Member GetUserInfo(int iPid)
        {
            Member userInfo = userModelService.GetMemberInforByPID(iPid);

            return userInfo;
        }

        public string getEReportDetail(int caseId, int orgId)
        {
            return userService.GetEReportDetail(caseId, orgId);
        }

        public string getEReportList(int caseId)
        {
            return userService.GetEReportList(caseId);
        }

        public string getReportDetail(int reportId, int orgid, string reportType)
        {
            return userService.GetReportDetail(reportId, orgid, reportType);
        }

        public string getCaseReportDetail(int reportId, int caseId, string reportType)
        {
            return userService.getCaseReportDetail(reportId,caseId,reportType);
        }

        public JEnumerable<JToken> GetDoctorListByNumber(string DocId)
        {
            return userService.GetDoctorListByNumber(DocId);
        }
    }
}
